from flask.ext.sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class LexDocType(db.Model):
	__tablename__ = 'lexdoctype'
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(40), unique=True)
	priority = db.Column(db.Integer, nullable=True)

	def __init__(self, name, priority):
		self.name = name
		self.priority = priority

	def __repr__(self):
		return '<LexDocType %r>' % self.name

class LexDoc(db.Model):
	__tablename__ = 'lexdoc'
	id = db.Column(db.Integer, primary_key=True)
	doctype_id = db.Column(db.Integer, db.ForeignKey('lexdoctype.id'))
	pubdate	= db.Column(db.DateTime)

	def __init__(self, doctype, pubdate=None):
		self.doctype = doctype
		if pubdate is None:
			pubdate = datetime.utcnow()
		self.pubdate = pubdate

	def __repr__(self):
		return '<LexDoc %r>' % self.id



class LexPartType(db.Model):
	__tablename__ = 'lexparttype'
	id = db.Column(db.Integer, primary_key=True)
	name = db.Column(db.String(40), unique=True)
	levelno = db.Column(db.Integer, nullable=True)

	def __init__(self, name, levelno):
		self.name = name
		self.levelno = levelno

	def __repr__(self):
		return '<LexPartType %r>' % self.name

class LexPart(db.Model):
	__tablename__ = 'lexpart'
	id = db.Column(db.Integer, primary_key=True)
	parttype_id = db.Column(db.Integer, db.ForeignKey('lexparttype.id'))
	partno = db.Column(db.String(15), nullable=True)
	validfrom = db.Column(db.DateTime)
	validtill = db.Column(db.DateTime, nullable=True)

	def __init__(self, parttypeid, partno=None, validfrom=None, validtill=None):
		self.parttype_id = parttypeid
		self.partno = partno
		self.validfrom = validfrom
		self.validtill = validtill

	def __repr__(self):
		return '<LexPart %r>' % self.id


class LexSequence(db.Model):
	__tablename__ = 'lexsequence'
	id = db.Column(db.Integer, primary_key=True)
	doc_id = db.Column(db.Integer, db.ForeignKey('lexdoc.id'), 
		primary_key=True)
	part_id = db.Column(db.Integer, db.ForeignKey('lexpart.id'),
		primary_key=True)
	seqno = db.Column(db.Integer)

	def __init__(self, docid, partid, seqno):
		self.doc_id = docid
		self.part_id = partid
		self.seqno = seqno

	def __repr__(self):
		return '<LexPartSequence %r>' % self.id


class LexPartText(db.Model):
	__tablename__ = 'lexparttext'
	id = db.Column(db.Integer, primary_key=True)
	part_id = db.Column(db.Integer, db.ForeignKey('lexpart.id'))
	language = db.Column(db.String(3))
	body = db.Column(db.Text)
	stamp = db.Column(db.DateTime)

	def __init__(self, partid, language, body, stamp=None):
		self.part_id = partid
		self.language = language
		self.body = body
		if stamp is None:
			stamp = datetime.utcnow()
		self.stamp = stamp

	def __repr__(self):
		return '<LexPartText %r>' % self.id


class LexPartTopic(db.Model):
	__tablename__ = 'lexparttopic'
	id = db.Column(db.Integer, primary_key=True)
	part_id = db.Column(db.Integer, db.ForeignKey('lexpart.id'))
	keyword = db.Column(db.String(80))

	def __init__(self, partid, keyword):
		self.part_id = partid
		self.keyword = keyword

	def __repr__(self):
		return '<LexPartTopic %r>' % self.id
